<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<link rel="stylesheet" type="text/css" href="../../css/demo.css" />
		<script src="../../js/demo.js" type="text/javascript" charset="utf-8"></script>
		<title>添加商品</title>
	</head>
	<body>
		<!--header-->
		<div class="header">
			<div class="header-content">
				<ul class="nav left">
					<li><img src="../../img/regist-arrow.png" /></li>
					<li class="nav-one">
						<a href="javascript:history.go(-1);">返回</a>
					</li>
					<li class="nav-two">
						<a href="../../">关闭</a>
					</li>
				</ul>
				<p class="left">添加商品</p>
				<div class="right nav_right">
					<img src="../../img/regist-list.png" />
				</div>
			</div>
		</div>
		<!--main-->
		<div class="main clear" style="margin-bottom: 1rem;">
			<div class="productaddmanagement">
				<div class="productaddmanagement-search">
					<img src="../../img/shopuser-icon4.png" class="left" onclick="goBack()"/>
					<div class="productaddmanage-search-M left">
						    <input type="text" name="goodDesc" id="productadd-search" placeholder="搜索您想要的商品"/>
							<img src="../../img/shopmanagement-icon9.png" id="i1"/>
					</div>
					<img src="../../img/product-add.png" class="right" onclick="location='../../html/divide/devide.html';"/>
				</div>	
			</div>
			
		<div class="productadd-foot">
			<ul class="productadd-footlist">
				<li class="productadd-footlist-one"><img src="../../img/shopaccount-icon2.png" style="margin-left: 0.2rem;" id="i6"/>全选</li>
				<li class="productadd-footlist-two ">
					<button id="b1" >添加商品(<span id="span1">0</span>)</button>
				</li>
			</ul>
		</div>
		</div>
		<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script type="text/javascript">
				//从地址栏中取出请求参数
				var search=location.search.substr(1);
				if(search!=""){
					$('input[name="goodDesc"]').val(decodeURI(search));
				}
				seacherProvideGood(decodeURI(search));
				
				//给选择框添加切换图片的功能
				//给全选按钮绑定click事件
				$('img[src="../../img/shopaccount-icon2.png"]').click(changePicture); 
				function changePicture(){
					var a = event.target;
					var c = $('#i6')[0];
					var b = $(a).attr('src');
					if (b == "../../img/shopaccount-icon2.png") {
						if (c == a) {
							$('img[src="../../img/shopaccount-icon2.png"]').attr('src',
									'../../img/shopaccount-icon.png');
						} else {
							$(a).attr('src', '../../img/shopaccount-icon.png');
						}
					} else {
						if (c == a) {
							$('img[src="../../img/shopaccount-icon.png"]').attr('src',
									'../../img/shopaccount-icon2.png');
						} else {
							$(a).attr('src', '../../img/shopaccount-icon2.png');
						}
							}
					}
				//获取存在div中的所有商品的provide_id,并拼接为字符串传递到后台
				function getId(){
					var array = $('img[src="../../img/shopaccount-icon.png"]');
					var dataStr = "1=1";
					var c = $('#i6')[0];
					for (var i = 0; i < array.length; i++) {
						if(array[i]!=c){
						dataStr += "&provideGoodId="+array[i].getAttribute('provideGoodId');
					}
						}
					return dataStr;
				}
				//添加商品按钮
				$('#b1').click(
					function(){
						var dataStr=getId();
						if(dataStr=="1=1"){
							alert("请选择要添加的商品！");
							return;
						}
						$.ajax({
							type:"post",
							url:"/qiaomengapp/shopGood/addGood",
							dataType:"json",
							data:dataStr,
							//controller层需要@responseBody，请求成功后才能执行success方法
							success:function(data){
								if(data.error!=undefined){
									alert(error);
									return;
								}
								//   -0 将String类型的a转换成number类型
								var a=$('#span1').html()-0;
								//获取a的数据类型
								a+=data;
								$('#span1').html(a);
								alert("添加成功");
							}
	
						});
					}		
				)
				
				//模糊搜索provide_good商品
				$('#i1').click(function(){
					//获取搜索的信息
					//.value取到是手动输入在输入框的内容。。。。。。attr("value")取的是input标签中的value属性的值
					var value=$('input[name="goodDesc"]').val();
					seacherProvideGood(value);
				})
				
				function seacherProvideGood(query){
						var dataStr="goodDesc="+query;					
						$.ajax({
							type:"post",
							url:"/qiaomengapp/provideGood/selectByGoodName",
							dataType:"json",
							data:dataStr,
							success:function(data){
								//去掉多余的div中的内容
								$('.productadd').remove();
								
								if(data==""){
									$(".productadd-foot").before("<div class='productadd'>要查询的商品不存在！</div>");
									return ;
								}
								
								//显示所有商品
								for(var i=0;i<data.length;i++){	
									
									$(".productadd-foot").before("<div class='productadd'>"+
											"<div class='productadd-checkox'>"
											+"<img src='../../img/shopaccount-icon2.png' onclick='changePicture()' provideGoodId=' "+data[i].id+"'/>" 
											+"<div class='productadd-list left'>"
											+"<img src='"+data[i].goodImgurl+"'/>"
											+"<ul class='productadd-listes left'>"
											+"<li class='productadd-one'>"+data[i].goodDesc
											+"</li>"
											+"<li class='productadd-two'>￥"+data[i].price+"<span>佣金：</span><span>￥"+data[i].commission+"</span></li>"
											+"<li class='productadd-thr'>剩余<span>"+(data[i].sum-data[i].salesCount)+"</span><span style='margin-right: 0.05rem;'>件</span>  销量<span style='margin-right: 0.1rem;'>"+data[i].salesCount+"</li>"
											+"</ul>"
											+"</div>"
											+"</div>"+
										"</div>");
								}
							}
						});
				}
				
				//history.go(-1)...返回上一个url地址
				//.val()中有值就是赋值，没值就是取值
				function goBack(){
					$('input[name="goodDesc"]').val("");
					showHtml();
 				}
				
		</script>
	</body>
</html>